------------------------------------------------
-- 道具格子
------------------------------------------------

local HeroExcel = require("excel.hero")
local EquipExcel = require("excel.equip")
local FuwenExcel = require("excel.fuwen")
local SkinExcel = require("excel.skin")
local Util = require("common.Util")
local ItemDefine = require("bag.ItemDefine")
local SuipianLogic = require("bag.SuipianLogic")
local RoleAttr = require("role.RoleAttr")
local EquipLogic = require("equip.EquipLogic")

-- 填充装备,道具信息 
function makeItem(net, itemID, itemCnt, shuijingAttrID, itemGrid, itemIndex, opFlag, equipQuality)
	local itemConfig = ItemDefine.getConfig(itemID)
	net.id = itemConfig and itemID or 0 
	net.cnt = itemConfig and itemCnt or 0
	net.name = itemConfig and itemConfig.name or ""
	net.mainType = itemConfig and itemConfig.mainType or 0
	net.subType = itemConfig and itemConfig.subType or 0
	net.order = itemConfig and itemConfig.order or 0
	net.icon = itemConfig and itemConfig.icon or 0
	net.desc1 = itemConfig and itemConfig.desc1 or ""
	net.desc2 = itemConfig and itemConfig.desc2 or ""
    local priceId = itemConfig and itemConfig.price and itemConfig.price[1] or 0
    local priceConfig = ItemDefine.getConfig(priceId)
    net.priceIcon = priceConfig and priceConfig.icon or 0
	net.price = itemConfig and itemConfig.price and itemConfig.price[2] or 0
	net.rare  = itemConfig and itemConfig.rare or 0
	net.score  = itemConfig and itemConfig.score or 0
	net.getway[0] = (itemConfig and itemConfig.getway) and #itemConfig.getway or 0	
	for i=1, net.getway[0] do
		net.getway[i] = itemConfig.getway[i]
	end
	net.panelID = itemConfig and itemConfig.panelID or 0
	net.isShow = itemConfig and itemConfig.isShow or 0
    net.effects = itemConfig and itemConfig.effects or 0
	net.opflag = opFlag or getOpflag(itemConfig) or 0
	net.suipian[0] = 0
    net.equip[0] = 0
    net.fuwen[0] = 0    
    if not itemConfig then return end

    makeItemSuipian(net, itemConfig)
    makeItemFuwen(net, itemID, itemConfig, itemGrid, itemIndex)
    makeItemEquip(net, itemConfig, itemGrid, itemIndex, shuijingAttrID, equipQuality)
end

function makeFuwenSkill(net, skillID)
	local skillConfig = FuwenExcel.skill[skillID]
	net.skillID = skillID
	net.skillIcon = skillConfig.icon 
	net.skillName = skillConfig.name 
	net.skillDesc = skillConfig.desc 
	net.lv = skillConfig.lv 
	net.isRare = skillConfig.rare or 0
	net.isOnlyBinshu = 0
	if skillConfig.isBingshuSkill == 1 and skillConfig.isFuwenSkill == 0 then
		net.isOnlyBinshu = 1
	end
end

function makeItemFuwen(net, itemID, itemConfig, fuwenGrid, fuwenIndex)
	if itemConfig.mainType ~= ItemDefine.MAINTYPE_FUWEN then
		return
	end
    local netTemp = nil
    if net.fuwen then
	    net.fuwen[0] = 1
        netTemp = net.fuwen[1]
    else
        netTemp = net
    end
	netTemp.id = itemID
	netTemp.index = fuwenIndex or 0
	netTemp.fenjieGet = itemConfig.fenjieJinghua
	netTemp.refine = 0
	netTemp.attrs[0] = 0
	netTemp.skills[0] = 0
	netTemp.zhandouli = 0
	if fuwenGrid ~= nil then
	    netTemp.refine = fuwenGrid.refine or 0
		local zhandouli = 0
		for zz = 1, #fuwenGrid.attr do
			local key = fuwenGrid.attr[zz][1]
			local value = fuwenGrid.attr[zz][2]
			netTemp.attrs[zz].key = key
			netTemp.attrs[zz].value = value
			zhandouli = zhandouli + RoleAttr.getPointByKey(key) * value
		end
		netTemp.attrs[0] = #fuwenGrid.attr
		if fuwenGrid.skill == nil then
			netTemp.skills[0] = 0
		else
			for zz = 1,#fuwenGrid.skill do
				local skillID = fuwenGrid.skill[zz]
				makeFuwenSkill(netTemp.skills[zz], skillID)
			end
			netTemp.skills[0] =  #fuwenGrid.skill
		end
		netTemp.zhandouli = math.floor(zhandouli)
	end
end

function makeItemEquip(net, itemConfig, equipGrid, equipIndex, shuijingAttrID, equipQuality)
	if itemConfig.mainType ~= ItemDefine.MAINTYPE_EQUIP then
		return
	end

	net.equip[0] = 1
    net.equip[1].index = equipIndex or 0
	net.equip[1].level = itemConfig.level
	net.equip[1].exp = itemConfig.shenqiExp or 0
    net.equip[1].star = itemConfig.star or 0
    net.equip[1].uuid = equipGrid and equipGrid.uuid or ""
    net.equip[1].putUuid = equipGrid and equipGrid.putUuid or ""
	net.equip[1].args[0] = 0

    local maxQuality = EquipLogic.getEquipMaxQuality(itemConfig)
	net.rare  = equipGrid and equipGrid.quality or equipQuality or maxQuality
	
	-- 装备基础属性
	if itemConfig.subType == ItemDefine.EQUIP_SUBTYPE_SHUIJIN then 
		-- 水晶属性
		net.equip[1].attr[0] = 0
		net.equip[1].args[0] = 1
		local upcf = EquipExcel.shuijingUpNeed[net.id]
		net.equip[1].args[1] = (not upcf or upcf.nextID < 1) and 1 or 0 -- 是否满级
		if shuijingAttrID then	
			local shuijingConfig = EquipExcel.shuijingAttr[shuijingAttrID]
			local shuijingAttrTB = shuijingConfig.attr
			net.equip[1].attr[0] = #shuijingAttrTB
			for i=1, net.equip[1].attr[0] do
				net.equip[1].attr[i].key = shuijingAttrTB[i][1]
				net.equip[1].attr[i].value = shuijingAttrTB[i][2]
			end
		end
	else
        local baseRate = EquipLogic.getEquipBaseRate(net.rare)
        net.score  = math.floor(net.score * baseRate)
        -- 基础属性
		net.equip[1].baseAttr[0] = #itemConfig.base
		for i=1, net.equip[1].baseAttr[0] do
			net.equip[1].baseAttr[i].key = itemConfig.base[i][1]
			net.equip[1].baseAttr[i].value = math.floor(itemConfig.base[i][2] * baseRate)
		end
        -- 随机属性
        local attrLen = 0
        if equipGrid then
           EquipLogic.checkAttr(equipGrid)
		   for key, value in pairs(equipGrid.attr) do
              attrLen = attrLen + 1
		   	  net.equip[1].tzAttr[attrLen].key = key
		   	  net.equip[1].tzAttr[attrLen].value = math.floor(value * baseRate) 
		   end
        else
           local randomAttr = EquipLogic.getEquipTzAttr(itemConfig)
           if randomAttr then
		      for _, attr in pairs(randomAttr) do
                 attrLen = attrLen + 1
		      	  net.equip[1].tzAttr[attrLen].key = attr[1]
		      	  net.equip[1].tzAttr[attrLen].value = attr[2]
		      end
           end
        end
        net.equip[1].tzAttr[0] = attrLen
	end
	if itemConfig.subType == ItemDefine.EQUIP_SUBTYPE_SHENQI then
		local shenqiUpNeedConfig = EquipExcel.shenqiUpNeed[net.id]
		local nextID = shenqiUpNeedConfig and shenqiUpNeedConfig.nextID
		net.equip[1].args[0] = 1
		net.equip[1].args[1] = (not nextID or nextID < 1) and 1 or 0
	end
	
	-- 装备套装属性
	net.equip[1].suit[0] = 0
	if itemConfig.suitIm then
		net.equip[1].suit[0] = 1
    	net.equip[1].suit[1].attr[0] = #itemConfig.suitIm
    	for i=1,net.equip[1].suit[1].attr[0] do
			net.equip[1].suit[1].attr[i].key = itemConfig.suitIm[i][1]
			net.equip[1].suit[1].attr[i].value = itemConfig.suitIm[i][2]
		end
		net.equip[1].suit[1].id = itemConfig.suit
    	net.equip[1].suit[1].name = itemConfig.suitName
    	net.equip[1].suit[1].num = itemConfig.suitNum
	end
	
	-- 装备职业专属属性
	net.equip[1].job = itemConfig.job
	net.equip[1].jobAttr[0] = #itemConfig.jobAttr
	for i = 1, net.equip[1].jobAttr[0] do
		net.equip[1].jobAttr[i].key = itemConfig.jobAttr[i][1]
		net.equip[1].jobAttr[i].value = itemConfig.jobAttr[i][2]
	end
	
	-- 装备阵营专属属性
	net.equip[1].camp = itemConfig.camp
	net.equip[1].campAttr[0] = #itemConfig.campAttr
	for i = 1, net.equip[1].campAttr[0] do
		net.equip[1].campAttr[i].key = itemConfig.campAttr[i][1]
		net.equip[1].campAttr[i].value = itemConfig.campAttr[i][2]
    end
end

-- 封装碎片信息 Suipian
function makeItemSuipian(net, itemConfig)
	if itemConfig.mainType ~= ItemDefine.MAINTYPE_ITEM then
		return
	end
	if itemConfig.subType ~= ItemDefine.ITEM_SUBTYPE_SUIPIAN and
		itemConfig.subType ~= ItemDefine.ITEM_SUBTYPE_SUIPIAN_SKIN then
		return
	end

	net.suipian[0] = 1
	net.suipian[1].fullcnt = itemConfig.fullCnt
	net.suipian[1].camp = itemConfig.cond[1] or 0
	net.suipian[1].star = itemConfig.cond[2] or 0
    net.suipian[1].get = itemConfig.get[1] or 0

	local heroID = itemConfig.get[3]
 	local heroConfig = HeroExcel.hero[heroID]

	net.suipian[1].grade = heroConfig and heroConfig.grade or 0

	if itemConfig.get[1] == SuipianLogic.SUMMON_TYPE_TARGET then 
		local dropType = itemConfig.get[2]
		if dropType == SuipianLogic.SUMMON_SUBTYPE_HERO then
			local heroID = itemConfig.get[3]
			local heroConfig = HeroExcel.hero[heroID]
			if heroConfig then 
				net.suipian[1].camp = heroConfig.camp  
				net.suipian[1].star = heroConfig.star 
				net.icon = heroConfig.head
			end
		end
	end
end 


--------------------------------------------- 可操作 -------------------------------------------------------
-- 可操作行为
function getOpflagAtBag(id)
    local itemConfig = ItemDefine.getConfig(id)
	if not itemConfig then return flag end
    if itemConfig then
       itemConfig.id = id
    end
    local flag = 0
    -- 使用道具
	if canUse(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_USE)
	end

    -- 使用箱子
    if canUseBox(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_USE_BOX)
	end

    -- 批量使用
	if canUseBatch(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_USE_BATCH)			
	end

    -- 详情
	if canDetail(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_DETAIL)
	end

    -- 是否可出售
    if canSell(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_SELL)
	end

    -- 是否用来源
    if canSource(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_SOURCE)
	end

      -- 是否可以合成
    if canHeCheng(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_HECHENG)
	end

    -- 能否穿戴
    if canEquip(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_EQUIP)
	end

    -- 能否重铸
    if canCZhu(itemConfig) then
        flag = Util.setBit(flag, ItemDefine.ITEM_OP_CZHU)
    end

    -- 能否分解
    if canFJie(itemConfig) then
        flag = Util.setBit(flag, ItemDefine.ITEM_OP_FJIE)
    end

	return flag
end

function getOpflag(itemConfig)
	local flag = 0
	if not itemConfig then return flag end

    -- 预览
	if canYulan(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_YULAN)
	end

    -- 是否用来源
    if canSource(itemConfig) then
		flag = Util.setBit(flag, ItemDefine.ITEM_OP_SOURCE)
	end

	return flag
end

-- 可否使用
function canUse(itemConfig)
	if not itemConfig then return end
    if not itemConfig.panelID then return end
	return itemConfig.panelID > 0 
end

-- 可否使用
function canUseBox(itemConfig)
	if not itemConfig then return end
	if not itemConfig.cmd then return end
	local cmdstr = itemConfig.cmd[1]
	if not cmdstr then return end
    if cmdstr == "box" then
		return true
	end
end

-- 是否可出售
function canSell(itemConfig)
	if not itemConfig then return end
	if not itemConfig.price then return end
	return #itemConfig.price > 0 
end

-- 是否可出售
function canSource(itemConfig)
	if not itemConfig then return end
	if not itemConfig.getway then return end
	return #itemConfig.getway > 0 
end


-- 可否批量使用
function canUseBatch(itemConfig)
	if not itemConfig then return end
	if not itemConfig.cmd then return end
	local cmdstr = itemConfig.cmd[1]
	if not cmdstr then return end
	if cmdstr == "box" then
		return
	end
	return true
end

-- 可否预览
function canYulan(itemConfig)
	if not itemConfig then return end
    local cmdStr = itemConfig.cmd and itemConfig.cmd[1]
	if cmdStr and cmdStr == "box" then
		return true
	end
    		
	--[[local getWay = itemConfig.get and itemConfig.get[1]
	if getWay == SuipianLogic.SUMMON_TYPE_DROP or
		getWay == SuipianLogic.SUMMON_TYPE_HERO_SP or
		getWay == SuipianLogic.SUMMON_TYPE_SHENQI_SP then	
		local suipianType = itemConfig.get[2]
		if suipianType == SuipianLogic.SUMMON_SUBTYPE_HERO or 
			suipianType == SuipianLogic.SUMMON_SUBTYPE_EQUIP then
			return true
		end
	end]]
end

-- 可否查看英雄详情
function canDetail(itemConfig)
	local getWay = itemConfig.get and itemConfig.get[1]
	if getWay == SuipianLogic.SUMMON_TYPE_TARGET then	
		local suipianType = itemConfig.get[2]
		if suipianType == SuipianLogic.SUMMON_SUBTYPE_HERO then
			local heroID = itemConfig.get[3]
			if HeroExcel.hero[heroID] then
				return true
			end
		end
		if suipianType == SuipianLogic.SUMMON_SUBTYPE_SKIN then
			local skinID = itemConfig.get[3]
			if SkinExcel.skin[skinID] then
				return true
			end
		end
	end
end


-- 可否合成
function canHeCheng(itemConfig)
	local getWay = itemConfig.get and itemConfig.get[1]
	if getWay == SuipianLogic.SUMMON_TYPE_TARGET or getWay == SuipianLogic.SUMMON_TYPE_DROP then	
		local suipianType = itemConfig.get[2]
		if suipianType == SuipianLogic.SUMMON_SUBTYPE_HERO then
           return true
		end
		if suipianType == SuipianLogic.SUMMON_SUBTYPE_SKIN then
           return true
		end
	end

    if ItemDefine.isFuwen(itemConfig.id) then
       return true
    end
end

-- 可否穿戴
function canEquip(itemConfig)
    if ItemDefine.isEquip(itemConfig.id) then
        return true
    end

    if ItemDefine.isFuwen(itemConfig.id) then
       return true
    end
end

-- 可否重铸
function canCZhu(itemConfig)
    if ItemDefine.isFuwen(itemConfig.id) then
       return true
    end
end

-- 可否重铸
function canFJie(itemConfig)
    if ItemDefine.isFuwen(itemConfig.id) then
       return true
    end
end





